package org.dg.iag.dao;

import org.dg.InitialServer;
import org.dg.iag.entity.User;
import org.dg.iag.entity.Users;
import org.dg.iag.interf.entity.UserResource;
import org.dg.iag.interf.entity.UsersResource;
import org.dg.iag.query.XQueryGet;
import org.dg.iag.query.XQueryPost;
import org.dg.iag.util.ApplicationConstants;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Protocol;
import org.restlet.ext.xstream.XstreamRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;

import com.thoughtworks.xstream.XStream;

public class UserDAO {
	Users users			= null;
	XStream xstream 	= null;
	
	
	public UserDAO() {
		xstream		= new XstreamRepresentation<User>(new User("test", "test")).getXstream(); 
	}
	
	public Users getUsers() {
		ClientResource cr = new ClientResource(InitialServer.configureContext(true), ApplicationConstants.ENTIRE_USERS_PATH);
		cr.setProtocol(Protocol.HTTP);
		cr.setMethod(Method.GET);
		cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, ApplicationConstants.EXIST_USER, ApplicationConstants.EXIST_PASSWORD);
//		System.out.println( "LALALA" +  cr.getContext().getParameters().getValues("maxConnectionsPerHost") );
//		try {
//			cr.get().write(System.out);
//		} catch (ResourceException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		// Get the Contact object
		UsersResource wrapper	= cr.wrap(UsersResource.class);
		Users users	= wrapper.getUsers();
		cr.release();
		return users;
	}
	
	public User getUser(String username) {
		XQueryGet xqg	= new XQueryGet(ApplicationConstants.ENTIRE_USERS_PATH, 
				"/Users/User[./username='"+username+"']", false); 	
		System.out.println("Getttings users from path: " + xqg);
		ClientResource cr = new ClientResource(InitialServer.configureContext(true), xqg.toString() );
		cr.setProtocol(Protocol.HTTP);
		cr.setMethod(Method.GET);
		cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, ApplicationConstants.EXIST_USER, ApplicationConstants.EXIST_PASSWORD);

		
		UserResource wrapper	= cr.wrap(UserResource.class);
		User user	= wrapper.getUser();
		cr.release();
		return user;
	}
	
	public void addUser(User u) {
		String userXml	= xstream.toXML(u);
		
		XQueryPost qp	= new XQueryPost();
		qp.setXmlns("http://exist.sourceforge.net/NS/exist");
		qp.setProperties(null);
		qp.setText("update insert " + userXml + " into /Users");
		
		ClientResource cr = new ClientResource(InitialServer.configureContext(true), ApplicationConstants.ENTIRE_USERS_PATH);
		cr.setProtocol(Protocol.HTTP);
		cr.setMethod(Method.POST);
		cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, ApplicationConstants.EXIST_USER, ApplicationConstants.EXIST_PASSWORD);
		//String qpXml			= xstream.toXML(qp);
		Representation qpRep	= new XstreamRepresentation<XQueryPost>(MediaType.TEXT_XML, qp);
		try {
			Representation result	= cr.post(qpRep);
			cr.release();
		} catch (ResourceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public void modifyUser(User u) {
		String userXml	= xstream.toXML(u);
		
		XQueryPost qp	= new XQueryPost();
		qp.setXmlns("http://exist.sourceforge.net/NS/exist");
		qp.setProperties(null);
		qp.setText("update replace /Users/User[./username='" + u.getUsername() + "'] with " + userXml);
		
		ClientResource cr = new ClientResource(InitialServer.configureContext(true), ApplicationConstants.ENTIRE_USERS_PATH);
		cr.setProtocol(Protocol.HTTP);
		cr.setMethod(Method.POST);
		cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, ApplicationConstants.EXIST_USER, ApplicationConstants.EXIST_PASSWORD);
		//String qpXml			= xstream.toXML(qp);
		Representation qpRep	= new XstreamRepresentation<XQueryPost>(MediaType.TEXT_XML, qp);
		try {
			Representation result	= cr.post(qpRep);
			cr.release();
		} catch (ResourceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
}
